package com.zhen;

import com.zhen.domain.Employee;
import com.zhen.mapper.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DynamicSQLTest {

    public static void main(String[] args) throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        DynamicSQLTest t = new DynamicSQLTest();
//        t.testSelectEmployeeByIdLike(session);
//        t.testSelectEmployeeByLoginLike(session);
//        t.testSelectEmployeeChoose(session);
//        t.testUpdateEmployeeIfNecessary(session);
//        t.testSelectEmployeeIn(session);
        t.testSelectEmployeeLikeName(session);

        session.commit();
        session.close();
    }

    public void testSelectEmployeeByIdLike(SqlSession session){
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Map<String,Object> params = new HashMap<>();
        params.put("id",1);

        List<Employee> employeeList = employeeMapper.selectEmployeeByIdLike(params);

        employeeList.forEach(employee -> System.out.println(employee));
    }

    public void testSelectEmployeeByLoginLike(SqlSession session){
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Map<String,Object> params = new HashMap<>();
        params.put("loginname","jack");
        params.put("password","123456");

        List<Employee> employeeList = employeeMapper.selectEmployeeByLoginLike(params);

        employeeList.forEach(employee -> System.out.println(employee));
    }

    public void testSelectEmployeeChoose(SqlSession session){
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);

        Map<String,Object> params = new HashMap<>();
        params.put("id","1");
        params.put("loginname","jack");
        params.put("password","123456");

        List<Employee> employeeList = employeeMapper.selectEmployeeChoose(params);

        employeeList.forEach(employee -> System.out.println(employee));
    }

    public void testUpdateEmployeeIfNecessary(SqlSession session){
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        Employee employee = employeeMapper.selectEmployeeWithId(4);

        employee.setLoginname("mary");
        employee.setPassword("123");
        employee.setName("玛丽");

        employeeMapper.updateEmployeeIfNecessary(employee);
    }

    public void testSelectEmployeeIn(SqlSession session){
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        List<Integer> ids = new ArrayList<>();
        ids.add(1);
        ids.add(2);
        List<Employee> list = employeeMapper.selectEmployeeIn(ids);
        list.forEach(employee -> System.out.println(employee));
    }

    public void testSelectEmployeeLikeName(SqlSession session){
        EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
        Employee employee = new Employee();
        employee.setName("o");
        List<Employee> list = employeeMapper.selectEmployeeLikeName(employee);
        System.out.println(list);
    }

}
